﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace WhileAwayWpf
{
    public class EventCountdownConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            TimeSpan timeLeft = (TimeSpan)value;

            if (timeLeft == TimeSpan.MinValue)
            {
                return "Sometime soon...";
            }
            else if (timeLeft.Seconds < 0)
            {
                return "I should be back by now!";
            }
            else
            {
                StringBuilder msgBuffer = new StringBuilder();
                if (timeLeft.Days > 0)
                {
                    msgBuffer.Append(timeLeft.Days);
                    msgBuffer.Append(" day(s)");
                }
                if (timeLeft.Hours > 0)
                {
                    if (msgBuffer.Length > 0)
                        msgBuffer.Append(", ");

                    msgBuffer.Append(timeLeft.Hours);
                    msgBuffer.Append(" hours(s)");
                }
                if (timeLeft.Minutes > 0)
                {
                    if (msgBuffer.Length > 0)
                        msgBuffer.Append(", ");

                    msgBuffer.Append(timeLeft.Minutes);
                    msgBuffer.Append(" minutes(s)");
                }
                if (timeLeft.Seconds > 0)
                {
                    if (msgBuffer.Length > 0)
                        msgBuffer.Append(", ");

                    msgBuffer.Append(timeLeft.Seconds);
                    msgBuffer.Append(" second(s)");
                }

                return msgBuffer.ToString();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

}
